    {{ $scr := $.Scratch }}

    {{/* Attempt to load local vendor JS, otherwise load from CDN. */}}
    {{ $cdn_js := site.Data.assets.cdn.js }}
    {{ $static_js := site.Data.assets.static.js }}

    

    {{ if not ($scr.Get "use_cdn") }}
      {{ partial "javascript_tag" (dict "key" "jquery") }}
      {{ partial "javascript_tag" (dict "key" "popper") }}
      {{ partial "javascript_tag" (dict "key" "imagesLoaded") }}
      {{ partial "javascript_tag" (dict "key" "isotope") }}
      {{ partial "javascript_tag" (dict "key" "fancybox") }}

      {{ if or .Params.diagram site.Params.diagram }}
        {{ partial "javascript_tag" (dict "key" "mermaid") }}
      {{ end }}

      {{ if $.Scratch.Get "highlight_enabled" }}
        {{ $v := $cdn_js.highlight.version }}
        {{ partial "javascript_tag" (dict "key" "highlight") }}
        {{ range site.Params.highlight_languages }}
          {{ partial "javascript_tag" (dict "filename" (printf "highlight-%s.min.js" .)) }}
        {{ end }}
      {{ end }}
    {{ else }}
      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $cdn_js.jQuery.url $cdn_js.jQuery.version) $cdn_js.jQuery.sri | safeHTML }}
      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $cdn_js.popper.url $cdn_js.popper.version) $cdn_js.popper.sri | safeHTML }}
      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $cdn_js.imagesLoaded.url $cdn_js.imagesLoaded.version) $cdn_js.imagesLoaded.sri | safeHTML }}
      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $cdn_js.isotope.url $cdn_js.isotope.version) $cdn_js.isotope.sri | safeHTML }}
      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $cdn_js.fancybox.url $cdn_js.fancybox.version) $cdn_js.fancybox.sri | safeHTML }}

      {{ if or .Params.diagram site.Params.diagram }}
        {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\" title=\"mermaid\"></script>" (printf $cdn_js.mermaid.url $cdn_js.mermaid.version) $cdn_js.mermaid.sri | safeHTML }}
      {{ end }}

      {{ if $.Scratch.Get "highlight_enabled" }}
        {{ $v := $cdn_js.highlight.version }}
        {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $cdn_js.highlight.url $v) $cdn_js.highlight.sri | safeHTML }}
        {{ range site.Params.highlight_languages }}
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/{{ $v }}/languages/{{ . }}.min.js"></script>
        {{ end }}
      {{ end }}

    {{ end }}

    {{/* Maps JS. */}}
    {{ if eq site.Params.map.engine 1 }}
      <script async defer src="https://maps.googleapis.com/maps/api/js?key={{ site.Params.map.api_key }}"></script>
      {{ if ($scr.Get "use_cdn") }}
      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $cdn_js.gmaps.url $cdn_js.gmaps.version) $cdn_js.gmaps.sri | safeHTML }}
      {{ end }}
    {{ else if and (or (eq site.Params.map.engine 2) (eq site.Params.map.engine 3)) ($scr.Get "use_cdn") }}
      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $cdn_js.leaflet.url $cdn_js.leaflet.version) $cdn_js.leaflet.sri | safeHTML }}
    {{ end }}

    {{/* Initialise code highlighting. */}}
    {{ if $scr.Get "highlight_enabled" }}
    <script>const code_highlighting = true;</script>
    {{ else }}
    <script>const code_highlighting = false;</script>
    {{ end }}

    {{/* Initialise default theme. */}}
    {{ if eq ($scr.Get "light") true }}
    <script>const isSiteThemeDark = false;</script>
    {{ else }}
    <script>const isSiteThemeDark = true;</script>
    {{ end }}

    {{ if ne site.Params.search.engine 0 }}
    {{/* Configure search engine. */}}
    {{ $min_length := site.Params.search.academic.min_length | default 1 }}
    {{ $threshold := site.Params.search.academic.threshold | default 0.3 }}
    {{ $search_i18n := dict "placeholder" (i18n "search_placeholder") "results" (i18n "search_results") "no_results" (i18n "search_no_results") }}
    {{ $search_config := dict "indexURI" ("/index.json" | relLangURL) "threshold" $threshold "minLength" $min_length }}
    <script>
      const search_config = {{ $search_config | jsonify | safeJS }};
      const i18n = {{ $search_i18n | jsonify | safeJS }};
      const content_type = {
        'post': {{ i18n "posts" }},
        'project': {{ i18n "projects" }},
        'publication' : {{ i18n "publications" }},
        'talk' : {{ i18n "talks" }}
        };
    </script>
    {{ end }}
    
    {{/* Load hash anchors for documentation pages. */}}
    {{ if eq .Type "docs" }}
    {{ if not ($scr.Get "use_cdn") }}
      {{ partial "javascript_tag" (dict "key" "anchor") }}
    {{ else }}
      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $cdn_js.anchor.url $cdn_js.anchor.version) $cdn_js.anchor.sri | safeHTML }}
    {{ end }}
    <script>
      anchors.add();
    </script>
    {{ end }}

    {{ if eq site.Params.search.engine 1 }}
    {{/* Fuse search result template. */}}
    <script id="search-hit-fuse-template" type="text/x-template">
      <div class="search-hit" id="summary-{{"{{key}}"}}">
      <div class="search-hit-content">
        <div class="search-hit-name">
          {{ printf "<a href=\"%s\">%s</a>" "{{relpermalink}}" "{{title}}" | safeHTML }}
          <div class="article-metadata search-hit-type">{{"{{type}}"}}</div>
          <p class="search-hit-description">{{"{{snippet}}"}}</p>
        </div>
      </div>
      </div>
    </script>
    {{ else if eq site.Params.search.engine 2 }}
    {{/* Algolia search result template. */}}
    <script id="search-hit-algolia-template" type="text/html">
      <div class="search-hit">
        <div class="search-hit-content">
          <div class="search-hit-name">
            {{ printf "<a href=\"%s\">{{{_highlightResult.title.value}}}</a>" "{{relpermalink}}" | safeHTML }}
          </div>
          <div class="article-metadata search-hit-type">{{"{{type}}"}}</div>
          <p class="search-hit-description">{{ safeHTML "{{{_highlightResult.summary.value}}}" }}</p>
        </div>
      </div>
    </script>
    {{ end }}

    {{/* Fuse search engine. */}}
    {{ if (eq site.Params.search.engine 1) }}
      {{ if not ($scr.Get "use_cdn") }}
        {{ partial "javascript_tag" (dict "key" "fuse") }}
        {{ partial "javascript_tag" (dict "key" "mark") }}
      {{ else }}
        {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $cdn_js.fuse.url $cdn_js.fuse.version) $cdn_js.fuse.sri | safeHTML }}
        {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $cdn_js.mark.url $cdn_js.mark.version) $cdn_js.mark.sri | safeHTML }}
      {{ end }}
    {{ end }}

    {{/* Algolia search engine. */}}
    {{ if eq site.Params.search.engine 2 }}
    {{ if ($scr.Get "use_cdn") }}
      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $cdn_js.instantsearch.url $cdn_js.instantsearch.version) $cdn_js.instantsearch.sri | safeHTML }}
    {{ end }}
    <script>
      const algoliaConfig = {
        appId: {{ site.Params.search.algolia.app_id }},
        apiKey: {{ site.Params.search.algolia.api_key }},
        indexName: {{ site.Params.search.algolia.index_name }},
        poweredBy: {{ site.Params.search.algolia.show_logo | default false }}
      };
    </script>
    {{ end }}

    {{/* Netlify Identity integration. */}}
    {{ if .IsHome | and (site.Params.cms.netlify_cms | default true) }}
    <script>
      if (window.netlifyIdentity) {
        window.netlifyIdentity.on("init", user => {
          if (!user) {
            window.netlifyIdentity.on("login", () => {
              document.location.href = "/admin/";
            });
          }
        });
      }
    </script>
    {{ end }}

    {{/* Disqus Comment Count JS. */}}
    {{ if and (eq site.Params.comments.engine 1) (site.Params.comments.disqus.show_count | default true) }}
    <script id="dsq-count-scr" src="https://{{site.Params.comments.disqus.shortname}}.disqus.com/count.js" async></script>
    {{ end }}

    {{ $js_comment := printf "/* Source Themes Academic v%s | https://sourcethemes.com/academic/ */\n" site.Data.academic.version }}
    {{ $js_bundle_head := $js_comment | resources.FromString "js/bundle-head.js" }}
    {{ $js_linebreak := "\n" | resources.FromString "js/linebreak.js" }}{{/* Fix no line break after Bootstrap JS causing error. */}}
    {{ $js_academic := resources.Get "js/academic.js" }}
    {{ $js_academic_search := resources.Get "js/academic-search.js" }}
    {{ $js_algolia_search := resources.Get "js/algolia-search.js" }}
    {{ $js_bootstrap := resources.Get "js/vendor/bootstrap.min.js" }}
    {{ $js_bundle := slice $js_bootstrap $js_linebreak $js_academic }}
    {{ if eq site.Params.search.engine 1 }}
      {{ $js_bundle = $js_bundle | append $js_academic_search }}
    {{ else if eq site.Params.search.engine 2 }}
      {{ $js_bundle = $js_bundle | append $js_algolia_search }}
    {{ end }}
    {{ range site.Params.plugins_js }}
      {{ $js_bundle = $js_bundle | append (resources.Get (printf "js/%s.js" .)) }}
    {{ end }}
    {{ $js_bundle := $js_bundle | resources.Concat "js/academic-bundle-pre.js" | minify }}
    {{ $js_bundle := slice $js_bundle_head $js_bundle | resources.Concat "js/academic.min.js" | fingerprint "md5" }}
    <script type="application/javascript" src="{{ $js_bundle.RelPermalink }}"></script>

    {{ partial "custom_js" . }}

    <script>
      var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
      tooltipTriggerList.map(function (tooltipTriggerEl) {
        return new window.bootstrap.Tooltip(tooltipTriggerEl)
      })
    </script>
